import BigDataTestAssistant
import DataProcessor
import os
import PyQt5.QtWidgets as Qw
import LogHandler


class InstructionGenerator(Qw.QWidget, BigDataTestAssistant.Ui_BDInsGenerater):
    def __init__(self):
        super().__init__()
        self.__collectionMode = ""
        self.__collectionTime = ""
        self.__signalFilePath = ""
        self.__generatedIns = ""
        self.setupUi(self)
        self.dataProcessor = DataProcessor.DataProcessor()
        self.logHandler = LogHandler.LogHandler()

        self.btn_SigSelection.clicked.connect(self.slot_set_file_path)
        self.btn_InsGen.clicked.connect(self.slot_generate_instruction)
        self.radioButton_EventMode.clicked.connect(self.slot_set_collection_mode)
        self.radioButton_CompleteMode.clicked.connect(self.slot_set_collection_mode)
        self.radioButton_PeriodicMode.clicked.connect(self.slot_set_collection_mode)

    def slot_set_file_path(self):
        self.__signalFilePath, _ = Qw.QFileDialog.getOpenFileName(self, "选取文件", os.getcwd(),
                                                             "All Files(*);;Excel Files(*.xlsx)")
        if self.__signalFilePath == "":
            self.logHandler.dummy_message("提示", "请选取信号文档")
            return

        self.dataProcessor.set_signal_file_path(self.__signalFilePath)
        self.InforViewer.append("选择了文件：" + self.__signalFilePath)

    def slot_set_collection_mode(self):
        if self.radioButton_PeriodicMode.isChecked():
            self.__collectionMode = "周期收集"
        elif self.radioButton_CompleteMode.isChecked():
            self.__collectionMode = "完整收集"
        else:
            self.__collectionMode = "触发收集"
        self.dataProcessor.set_col_mode(self.__collectionMode)
        self.InforViewer.append("选择了模式：" + self.__collectionMode)

    def check_condition(self) -> bool:
        if self.__signalFilePath == "":
            self.logHandler.dummy_message("提示", "请选取信息文件")
            return False
        elif self.__collectionMode == "":
            self.logHandler.dummy_message("提示", "请选取收集模式")
            return False
        return True

    def slot_generate_instruction(self):
        if not self.check_condition():
            return
        # TODO: 1. disable radio buttons
        self.__generatedIns = self.dataProcessor.get_instruction()
        self.InforViewer.append("生成的指令：" + self.__generatedIns + "\n"+">"*30)


